package net.spider.tasks.sankaku;

import java.io.File;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import net.cl.utils.FileUtil;
import net.cl.utils.SystemUtil;
import net.spider.Constants;
import net.spider.dao.SankakuDao;
import net.spider.entity.ContentEntity;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class AnalyzeTask extends Thread {

	private static final Log log = LogFactory.getLog(AnalyzeTask.class);
	private static final String FOLD = "E:/Downloads/sankaku/%s";
	public static final BlockingQueue<ContentEntity> QUEUE = new LinkedBlockingQueue<ContentEntity>();
	public static boolean END = false;
	private String url;

	public void run() {
		log.info("sankaku.AnalyzeTask started");
		while (true) {
			if (TrunkTask.QUEUE.isEmpty()) {
				if (isEnd()) {
					END = true;
					break;
				} else {
					try {
						Thread.sleep(1000);
						log.info("AnalyzeTask sleep.");
					} catch (InterruptedException e) {
					}
					continue;
				}
			}
			try {
				ContentEntity entity = TrunkTask.QUEUE.take();
				String id = entity.getId();
				if (!SankakuDao.isExists(id)) {
					url = entity.getUrl();
					log.info(url);
					Document doc = Jsoup.connect(url).userAgent(Constants.USER_AGENT).timeout(20000).get();
					Element e = doc.getElementById("postcontent");
					Elements links = e.select("a[class=highslide]");
					StringBuilder sb = new StringBuilder();
					if (links != null) {
						for (Iterator<Element> iterator = links.iterator(); iterator.hasNext();) {
							Element element = iterator.next();
							if (sb.length() > 0)
								sb.append(SystemUtil.getLineSeparator());
							sb.append(element.attr("href"));
						}
					}
					File f = FileUtil.newFolder(String.format(FOLD, id));
					entity.setFolder(f.getAbsolutePath());
					String path = entity.getFolder() + "/links.txt";
					FileUtil.write(path, sb.toString());
					putQueue(entity);
				} else
					log.info(entity.getUrl() + " has analyzed.");
			} catch (Exception e) {
				log.error(url, e);
			}
		}
		log.info("sankaku.AnalyzeTask end");
	}

	public static synchronized void putQueue(ContentEntity entity) {
		try {
			QUEUE.put(entity);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public boolean isEnd() {
		return TrunkTask.END;
	}

}
